package com.apps.globals.color.model.vo;

import lombok.Data;

@Data
public class HSV {

    private double H;

    private double S;

    private double V;


    public HSV(int red, int green , int blue )
    {
        double 	maxRGB	=	max( red , green , blue);//
        double	minRGB	=	min( red , green , blue);
        double	itemp	=	maxRGB;            //v'=itemp
        double	temp	=	maxRGB	-	minRGB;//

        if( maxRGB == minRGB )
        {
            this.H = 0;
            this.S = 0;
            this.V = maxRGB / 255;
            return;
        }
        double	rtemp	=	( itemp - red )	/ temp;//r'=rtemp
        double	gtemp	=	( itemp - green ) / temp;//g'=gtemp
        double	btemp	=	( itemp - blue ) / temp;//b'=btemp
        this.V = itemp / 255;//v=this.vHSV
        this.S = temp / itemp;//s'=this.sHSV
        if( red == maxRGB ){
            if( green == minRGB )
                this.H = 5 + btemp;
            else
                this.H = 1 - gtemp;
        }
        else if( green == maxRGB ){
            if( blue == minRGB )
                this.H = 1 + rtemp;
            else
                this.H = 3 - btemp;
        }
        else if( blue == maxRGB ){
            if( red == minRGB )
                this.H = 3 + gtemp;
            else
                this.H = 5 - rtemp;
        }
        this.H *=60;

    }

    public HSV(RGB rgb)
    {
        HSV hsv=new HSV(rgb.getR(),rgb.getG(),rgb.getB());
        this.H=hsv.H;
        this.S = hsv.S;
        this.V = hsv.V;
    }



    private int max(int... r)
    {
        int result=0;
        for(int item:r)
        {
            if(item>result)
                result = item;
        }
        return result;
    }

    private int min(int... r)
    {
        int result=255;
        for(int item:r)
        {
            if(item<result)
                result = item;
        }
        return result;
    }

}
